<!DOCTYPE html>
<html lang='zh-CN'>
<head>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>飞书管理页 - 系统配置</title>
    <style>
        :root {
            --primary: #3b82f6;
            --primary-dark: #1e40af;
            --success: #10b981;
            --warning: #f59e0b;
            --danger: #ef4444;
            --bg: #f8fafc;
            --card: #ffffff;
            --border: #e2e8f0;
            --text: #1e293b;
            --text-muted: #64748b;
            --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
            --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
        }
        
        * { box-sizing: border-box; margin: 0; padding: 0; }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: var(--text);
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 2rem 1rem;
        }
        
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            padding: 1.5rem 2rem;
            color: white;
        }
        
        .header-left h1 {
            font-size: 1.875rem;
            font-weight: 700;
            margin-bottom: 0.25rem;
        }
        
        .header-left p {
            opacity: 0.8;
            font-size: 0.875rem;
        }
        
        .header-right {
            display: flex;
            gap: 1rem;
            align-items: center;
        }
        
        .back-btn {
            padding: 0.5rem 1rem;
            background: rgba(255, 255, 255, 0.2);
            color: white;
            text-decoration: none;
            border-radius: 8px;
            font-size: 0.875rem;
            transition: all 0.2s;
        }
        
        .back-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        
        .setup-grid {
            display: grid;
            grid-template-columns: 1fr;
            gap: 2rem;
            margin-bottom: 2rem;
        }
        
        .card {
            background: var(--card);
            border-radius: 16px;
            padding: 2rem;
            box-shadow: var(--shadow-lg);
        }
        
        .card-header {
            display: flex;
            align-items: center;
            gap: 1rem;
            margin-bottom: 1.5rem;
        }
        
        .step-number {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            background: var(--primary);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 0.875rem;
        }
        
        .card-title {
            font-size: 1.125rem;
            font-weight: 600;
            color: var(--text);
            margin-bottom: 0.25rem;
        }
        
        .card-desc {
            font-size: 0.875rem;
            color: var(--text-muted);
        }
        
        .form-section {
            margin-bottom: 1.5rem;
        }
        
        .form-label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 600;
            color: var(--text);
        }
        
        .form-row {
            display: flex;
            gap: 0.75rem;
            margin-bottom: 1rem;
        }
        
        .form-input {
            flex: 1;
            padding: 0.75rem 1rem;
            border: 1px solid var(--border);
            border-radius: 8px;
            background: white;
            color: var(--text);
            font-size: 0.875rem;
            outline: none;
            transition: border-color 0.2s;
        }
        
        .form-input:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgb(59 130 246 / 0.1);
        }
        
        .btn {
            padding: 0.75rem 1.5rem;
            border: none;
            border-radius: 8px;
            font-size: 0.875rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            text-decoration: none;
        }
        
        .btn-primary {
            background: var(--primary);
            color: white;
        }
        
        .btn-primary:hover {
            background: var(--primary-dark);
        }
        
        .btn-outline {
            background: transparent;
            color: var(--primary);
            border: 1px solid var(--primary);
        }
        
        .btn-outline:hover {
            background: var(--primary);
            color: white;
        }
        
        .actions-row {
            display: flex;
            gap: 0.75rem;
            align-items: center;
        }
        
        .hint {
            font-size: 0.75rem;
            color: var(--text-muted);
            margin-top: 0.5rem;
            line-height: 1.4;
        }
        
        .help-text {
            background: #f0f9ff;
            border-left: 4px solid var(--primary);
            padding: 1rem;
            margin: 1rem 0;
            border-radius: 0 8px 8px 0;
            font-size: 0.875rem;
        }
        
        .help-text h4 {
            margin-bottom: 0.5rem;
            color: var(--primary);
        }
        
        .help-text ol {
            padding-left: 1.25rem;
        }
        
        .help-text li {
            margin-bottom: 0.25rem;
        }
        
        .output-card {
            border-top: 4px solid var(--primary);
        }
        
        .output-area {
            background: #f8fafc;
            border: 1px solid var(--border);
            border-radius: 8px;
            padding: 1rem;
            font-family: 'Courier New', monospace;
            font-size: 0.875rem;
            white-space: pre-wrap;
            max-height: 400px;
            overflow-y: auto;
        }
        
        .toast {
            position: fixed;
            top: 20px;
            right: 20px;
            background: white;
            padding: 1rem 1.5rem;
            border-radius: 8px;
            box-shadow: var(--shadow-lg);
            opacity: 0;
            transform: translateX(100%);
            transition: all 0.3s ease;
            z-index: 1000;
            min-width: 300px;
        }
        
        .toast.show {
            opacity: 1;
            transform: translateX(0);
        }
        
        .toast.success {
            border-left: 4px solid var(--success);
        }
        
        .toast.error {
            border-left: 4px solid var(--danger);
        }
        
        @media (max-width: 768px) {
            .setup-grid {
                grid-template-columns: 1fr;
            }
            
            .form-row {
                flex-direction: column;
            }
            
            .actions-row {
                flex-direction: column;
                align-items: stretch;
            }
        }
    </style>
</head>
<body>
    <div class='container'>
        <div class='header'>
            <div class='header-left'>
                <h1>⚙️ 系统管理</h1>
                <p>配置飞书应用凭证、管理审批流程、生成代码模板</p>
            </div>
            <div class='header-right'>
                <a href='/api/docs' class='back-btn' style='margin-right: 1rem;'>📚 API文档</a>
                <a href='{{PathPrefix}}' class='back-btn'>← 返回控制面板</a>
            </div>
        </div>
        
        <div class='setup-grid'>
            <!-- 管理口令设置/验证 -->
            <div class='card' id='passwordCard'>
                <div class='card-header'>
                    <div class='step-number'>1</div>
                    <div>
                        <div class='card-title' id='passwordTitle'>管理口令设置</div>
                        <div class='card-desc' id='passwordDesc'>首次使用，请设置管理口令以保护系统安全</div>
                    </div>
                </div>
                
                <!-- 初始化密码界面 -->
                <div class='form-section' id='initPasswordSection'>
                    <div class='form-row'>
                        <input id='newPwd' type='password' placeholder='设置管理口令（至少6位）' class='form-input'>
                        <input id='confirmPwd' type='password' placeholder='确认管理口令' class='form-input'>
                    </div>
                    <div class='form-row'>
                        <button class='btn btn-primary' onclick='initPassword()'>
                            🔒 设置口令
                        </button>
                    </div>
                    <div class='hint'>请妥善保管您的管理口令，遗失后需要重新部署系统</div>
                </div>

                <!-- 验证密码界面 -->
                <div class='form-section' id='verifyPasswordSection' style='display: none;'>
                    <div class='form-row'>
                        <input id='pwd' type='password' placeholder='输入管理口令进行验证' class='form-input'>
                        <button class='btn btn-outline' onclick='verifyPwd()'>
                            🔍 验证口令
                        </button>
                    </div>
                    <div class='form-row'>
                        <button class='btn btn-outline' onclick='showChangePassword()' style='margin-top: 0.5rem;'>
                            🔑 修改口令
                        </button>
                    </div>
                    <div class='hint'>管理口令用于保护系统配置安全</div>
                </div>

                <!-- 修改密码界面 -->
                <div class='form-section' id='changePasswordSection' style='display: none;'>
                    <div class='form-row'>
                        <input id='oldPwd' type='password' placeholder='当前管理口令（如未设置可留空）' class='form-input'>
                    </div>
                    <div class='form-row'>
                        <input id='newPwdChange' type='password' placeholder='新管理口令（至少6位）' class='form-input'>
                        <input id='confirmPwdChange' type='password' placeholder='确认新管理口令' class='form-input'>
                    </div>
                    <div class='form-row'>
                        <button class='btn btn-primary' onclick='changePassword()'>
                            🔄 设置/修改口令
                        </button>
                        <button class='btn btn-outline' onclick='cancelChangePassword()'>
                            ❌ 取消
                        </button>
                    </div>
                    <div class='hint'>首次设置时可不填写当前口令，修改后请妥善保管新的管理口令</div>
                </div>
            </div>

            <!-- 飞书应用配置 -->
            <div class='card'>
                <div class='card-header'>
                    <div class='step-number'>2</div>
                    <div>
                        <div class='card-title'>飞书应用配置</div>
                        <div class='card-desc'>配置飞书开放平台的应用凭证</div>
                    </div>
                </div>
                
                <div class='form-section'>
                    <label class='form-label'>App ID (必填)</label>
                    <div class='form-row'>
                        <input id='appId' placeholder='cli_a1234567890abcde' class='form-input'>
                    </div>
                    
                    <div class='help-text'>
                        <h4>📱 如何获取 App ID？</h4>
                        <ol>
                            <li>登录 <a href='https://open.feishu.cn/app' target='_blank'>飞书开放平台</a></li>
                            <li>选择你的应用（如果没有应用，点击"创建应用"）</li>
                            <li>进入"应用信息" → "基础信息"</li>
                            <li>复制"App ID"字段的值（格式：cli_xxxxxxx）</li>
                        </ol>
                    </div>
                </div>
                
                <div class='form-section'>
                    <label class='form-label'>App Secret (必填)</label>
                    <div class='form-row'>
                        <input id='appSecret' placeholder='应用密钥' type='password' class='form-input'>
                    </div>
                    
                    <div class='help-text'>
                        <h4>🔐 如何获取 App Secret？</h4>
                        <ol>
                            <li>在飞书开放平台的应用页面</li>
                            <li>进入"应用信息" → "基础信息"</li>
                            <li>找到"App Secret"字段，点击"查看"按钮</li>
                            <li>复制显示的密钥值</li>
                        </ol>
                        <p><strong>⚠️ 注意：</strong> App Secret 是敏感信息，请妥善保管</p>
                    </div>
                </div>
                
                <div class='form-section'>
                    <label class='form-label'>可选配置</label>
                    <div class='form-row'>
                        <input id='encryptKey' placeholder='消息加密Key (可选)' class='form-input'>
                        <input id='vt' placeholder='事件订阅Token (可选)' class='form-input'>
                    </div>
                    <div class='hint'>如果只用于发起审批，可选配置项可以留空</div>
                </div>
                
                <div class='actions-row'>
                    <button class='btn btn-primary' onclick='saveCfg()'>
                        💾 保存配置
                    </button>
                    <button class='btn btn-outline' onclick='checkCfg()'>
                        🔍 检测状态
                    </button>
                </div>
            </div>

            <!-- 审批流程登记 -->
            <div class='card'>
                <div class='card-header'>
                    <div class='step-number'>3</div>
                    <div>
                        <div class='card-title'>审批流程登记</div>
                        <div class='card-desc'>登记飞书审批的 approval_code</div>
                    </div>
                </div>
                
                <div class='help-text'>
                    <h4>📋 如何获取审批代码？</h4>
                    <ol>
                        <li>登录 <a href='https://www.feishu.cn/approval/create' target='_blank'>飞书审批管理后台</a></li>
                        <li>创建或找到你的审批应用</li>
                        <li>在应用设置中找到"审批代码"（UUID格式）</li>
                        <li>确保飞书应用已开通"审批"API权限</li>
                    </ol>
                </div>
                
                <div class='form-section'>
                    <div class='form-row'>
                        <input id='approvalCode' placeholder='7C468A54-8745-2245-9675-08DC917F109D' class='form-input'>
                        <input id='displayName' placeholder='显示名称 (如：请假申请)' class='form-input'>
                    </div>
                </div>
                
                <div class='actions-row'>
                    <button class='btn btn-primary' onclick='reg()'>
                        📝 登记审批
                    </button>
                    <button class='btn btn-outline' onclick='loadRegistrations()'>
                        📋 查看已登记
                    </button>
                </div>
            </div>

            <!-- 代码生成器 -->
            <div class='card'>
                <div class='card-header'>
                    <div class='step-number'>4</div>
                    <div>
                        <div class='card-title'>代码生成器</div>
                        <div class='card-desc'>自动生成 C# 请求实体类</div>
                    </div>
                </div>
                
                <div class='form-section'>
                    <label class='form-label'>审批代码 (必填)</label>
                    <div class='form-row'>
                        <input id='code' placeholder='如: leave_approval_001' class='form-input'>
                    </div>
                    
                    <label class='form-label'>生成的类名 (可选)</label>
                    <div class='form-row'>
                        <input id='className' placeholder='如: LeaveApprovalRequest (留空使用默认名称)' class='form-input'>
                        <button class='btn btn-primary' onclick='gen()'>
                            🚀 生成代码
                        </button>
                    </div>
                    <div class='hint'>
                        <strong>类名规范：</strong> 使用有意义的名称，如 LeaveApprovalRequest、ExpenseReimbursementRequest 等<br>
                        <strong>审批代码：</strong> 从飞书审批定义中获取，通过 [ApprovalCode] 特性关联
                    </div>
                </div>
            </div>

            <!-- 输出结果 -->
            <div class='card output-card'>
                <div class='card-header'>
                    <div style='width: 32px; height: 32px; border-radius: 50%; background: #f3f4f6; display: flex; align-items: center; justify-content: center; font-size: 1.25rem;'>📤</div>
                    <div>
                        <div class='card-title'>输出结果</div>
                        <div class='card-desc'>操作结果和生成的代码将在此显示</div>
                    </div>
                </div>
                
                <div id='output' class='output-area'>
等待操作...
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // 运行时补丁：支持在未经过后端模板渲染（例如直接用文件协议或IDE静态服务）时，通过查询参数 apiBase 或后端固定配置接口获取 ApiPrefix
        (function(){
            const originalFetch = window.fetch.bind(window);
            const urlParams = new URLSearchParams(location.search);
            const queryApiBase = urlParams.get('apiBase');

            // 简单的占位符检测
            const isTemplateUnrendered = function(str){
                return typeof str === 'string' && str.indexOf('{{ApiPrefix}}') >= 0;
            };

            // 获取后端配置（当通过 http(s) 访问且未提供 apiBase 时）
            async function tryResolveApiBaseFromServer() {
                try {
                    // 尝试从当前页面推断出 managePath 前缀，回退到 /feishu
                    const base = location.origin;
                    // 默认路径：/feishu/api/dashboard/config
                    const fallback = base + '/feishu/api/dashboard/config';
                    // 如果当前路径里包含 /feishu/manage，则替换为 /feishu/api/dashboard/config
                    let guess = '';
                    if (location.pathname.includes('/manage')) {
                        guess = base + location.pathname.replace('/manage', '/api/dashboard/config');
                    }
                    const probe = guess || fallback;
                    const resp = await originalFetch(probe);
                    if (!resp.ok) throw new Error('config http ' + resp.status);
                    const data = await resp.json();
                    if (data && data.apiPrefix) return data.apiPrefix;
                } catch(_) { /* 忽略，走回退 */ }
                return '/feishu/api';
            }

            let resolvedApiBase = queryApiBase || null;

            window.fetch = async function(resource, init){
                try {
                    if (typeof resource === 'string' && isTemplateUnrendered(resource)) {
                        if (!resolvedApiBase) {
                            // 仅当 http(s) 场景才尝试请求服务端配置
                            if (location.origin && location.origin.startsWith('http')) {
                                resolvedApiBase = await tryResolveApiBaseFromServer();
                            }
                            resolvedApiBase = resolvedApiBase || '/feishu/api';
                        }
                        resource = resource.replace('{{ApiPrefix}}', resolvedApiBase);
                    }
                } catch(_) { /* 安全保护 */ }
                return originalFetch(resource, init);
            };
        })();

        let currentPassword = '';
        let hasAdminPassword = false;
        
        // 页面加载时检查密码状态
        window.onload = async function() {
            await checkPasswordStatus();
        };
        
        // 检查管理员密码状态
        async function checkPasswordStatus() {
            try {
                const response = await fetch('{{ApiPrefix}}/admin-password/status');
                const result = await response.json();
                hasAdminPassword = result.hasPassword;
                
                if (hasAdminPassword) {
                    // 已设置密码，显示验证界面
                    document.getElementById('passwordTitle').textContent = '管理口令验证';
                    document.getElementById('passwordDesc').textContent = '输入管理口令以进行系统配置';
                    document.getElementById('initPasswordSection').style.display = 'none';
                    document.getElementById('verifyPasswordSection').style.display = 'block';
                } else {
                    // 未设置密码，显示初始化界面
                    document.getElementById('passwordTitle').textContent = '管理口令设置';
                    document.getElementById('passwordDesc').textContent = '首次使用，请设置管理口令以保护系统安全';
                    document.getElementById('initPasswordSection').style.display = 'block';
                    document.getElementById('verifyPasswordSection').style.display = 'none';
                }
                document.getElementById('changePasswordSection').style.display = 'none';
            } catch (error) {
                console.error('检查密码状态失败:', error);
                showToast('检查密码状态失败: ' + error.message, 'error');
            }
        }
        
        function showToast(message, type) {
            const toast = document.createElement('div');
            toast.className = 'toast ' + (type || 'success');
            toast.innerHTML = '<div style="font-weight: 500; margin-bottom: 0.25rem;">' + (type === 'error' ? '❌ 错误' : '✅ 成功') + '</div><div>' + message + '</div>';
            document.body.appendChild(toast);
            
            setTimeout(function() { toast.classList.add('show'); }, 100);
            setTimeout(function() {
                toast.classList.remove('show');
                setTimeout(function() { document.body.removeChild(toast); }, 300);
            }, 3000);
        }
        
        function updateOutput(content, type) {
            const output = document.getElementById('output');
            output.textContent = typeof content === 'string' ? content : JSON.stringify(content, null, 2);
            output.className = 'output-area';
        }
        
        // 初始化密码
        async function initPassword() {
            const newPwd = document.getElementById('newPwd').value;
            const confirmPwd = document.getElementById('confirmPwd').value;
            
            if (!newPwd || !newPwd.trim()) {
                showToast('请输入新密码', 'error');
                return;
            }
            
            if (newPwd.length < 6) {
                showToast('密码长度至少6位', 'error');
                return;
            }
            
            if (newPwd !== confirmPwd) {
                showToast('两次输入的密码不一致', 'error');
                return;
            }
            
            try {
                const response = await fetch('{{ApiPrefix}}/admin/password', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        password: newPwd
                    })
                });
                
                if (response.ok) {
                    showToast('管理口令设置成功');
                    currentPassword = newPwd;
                    hasAdminPassword = true;
                    updateOutput('✅ 管理口令已设置并缓存\n\n现在可以进行配置保存和审批登记操作');
                    await checkPasswordStatus(); // 刷新界面状态
                } else {
                    const errorText = await response.text();
                    showToast('设置失败: ' + errorText, 'error');
                    updateOutput('❌ 设置失败: ' + errorText);
                }
            } catch (error) {
                showToast('网络错误: ' + error.message, 'error');
                updateOutput('❌ 网络错误: ' + error.message);
            }
        }

        function verifyPwd() {
            const pwd = document.getElementById('pwd').value;
            if (!pwd || !pwd.trim()) {
                showToast('请输入管理口令', 'error');
                return;
            }
            
            currentPassword = pwd;
            showToast('口令已缓存，可用于后续操作');
            updateOutput('✅ 管理口令已验证并缓存\n\n现在可以进行配置保存和审批登记操作', 'success');
        }

        // 显示修改密码界面
        function showChangePassword() {
            document.getElementById('verifyPasswordSection').style.display = 'none';
            document.getElementById('changePasswordSection').style.display = 'block';
            document.getElementById('passwordTitle').textContent = '修改管理口令';
            document.getElementById('passwordDesc').textContent = '请输入当前口令和新口令';
        }

        // 取消修改密码
        function cancelChangePassword() {
            document.getElementById('changePasswordSection').style.display = 'none';
            document.getElementById('verifyPasswordSection').style.display = 'block';
            document.getElementById('passwordTitle').textContent = '管理口令验证';
            document.getElementById('passwordDesc').textContent = '输入管理口令以进行系统配置';
            // 清空输入框
            document.getElementById('oldPwd').value = '';
            document.getElementById('newPwdChange').value = '';
            document.getElementById('confirmPwdChange').value = '';
        }

        // 修改密码
        async function changePassword() {
            const oldPwd = document.getElementById('oldPwd').value;
            const newPwd = document.getElementById('newPwdChange').value;
            const confirmPwd = document.getElementById('confirmPwdChange').value;
            
            if (!newPwd || !newPwd.trim()) {
                showToast('请输入新密码', 'error');
                return;
            }
            
            if (newPwd.length < 6) {
                showToast('新密码长度至少6位', 'error');
                return;
            }
            
            if (newPwd !== confirmPwd) {
                showToast('两次输入的新密码不一致', 'error');
                return;
            }
            
            try {
                // 先检查是否已设置密码
                const statusResponse = await fetch('{{ApiPrefix}}/admin-password/status', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                
                if (!statusResponse.ok) {
                    showToast('检查密码状态失败', 'error');
                    return;
                }
                
                const statusData = await statusResponse.json();
                const hasPassword = statusData.hasPassword;
                
                // 如果已设置密码，需要验证旧密码
                if (hasPassword) {
                    if (!oldPwd || !oldPwd.trim()) {
                        // 非必须提示：不再弹出错误，仅聚焦输入框
                        document.getElementById('oldPwd').focus();
                        return;
                    }
                    
                    // 验证旧密码
                    const verifyResponse = await fetch('{{ApiPrefix}}/admin/password/verify', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            password: oldPwd
                        })
                    });
                    
                    if (!verifyResponse.ok || !(await verifyResponse.json())) {
                        showToast('当前密码错误', 'error');
                        return;
                    }
                }
                
                // 设置新密码
                const response = await fetch('{{ApiPrefix}}/admin/password', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        password: newPwd
                    })
                });
                
                if (response.ok) {
                    const successMessage = hasPassword ? '管理口令修改成功' : '管理口令设置成功';
                    showToast(successMessage);
                    currentPassword = newPwd;
                    updateOutput(`✅ ${successMessage}并缓存\n\n现在可以使用新口令进行系统配置`);
                    cancelChangePassword(); // 返回验证界面
                } else {
                    const errorText = await response.text();
                    showToast('操作失败: ' + errorText, 'error');
                    updateOutput('❌ 操作失败: ' + errorText);
                }
            } catch (error) {
                showToast('网络错误: ' + error.message, 'error');
                updateOutput('❌ 网络错误: ' + error.message);
            }
        }
        
        async function saveCfg() {
            const pwd = currentPassword || document.getElementById('pwd').value;
            if (!pwd || !pwd.trim()) {
                showToast('请先设置管理口令', 'error');
                return;
            }
            
            const config = {
                appId: document.getElementById('appId').value || '',
                appSecret: document.getElementById('appSecret').value || '',
                encryptKey: document.getElementById('encryptKey').value || '',
                verificationToken: document.getElementById('vt').value || ''
            };
            
            if (!config.appId.trim() || !config.appSecret.trim()) {
                showToast('AppId 和 AppSecret 不能为空', 'error');
                return;
            }
            
            try {
                const response = await fetch('{{ApiPrefix}}/config', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-Feishu-Admin-Password': pwd
                    },
                    body: JSON.stringify(config)
                });
                
                if (response.ok) {
                    showToast('飞书配置保存成功');
                    updateOutput('✅ 飞书应用配置已更新\n\n配置详情:\nApp ID: ' + config.appId + '\nApp Secret: ********\nEncrypt Key: ' + (config.encryptKey || '(未设置)') + '\nVerification Token: ' + (config.verificationToken || '(未设置)'));
                } else if (response.status === 401) {
                    showToast('管理口令验证失败', 'error');
                    updateOutput('❌ 认证失败: 管理口令不正确');
                } else {
                    showToast('保存失败，请重试', 'error');
                    updateOutput('❌ 保存失败: HTTP ' + response.status);
                }
            } catch (error) {
                showToast('网络错误: ' + error.message, 'error');
                updateOutput('❌ 网络错误: ' + error.message);
            }
        }
        
        async function checkCfg() {
            try {
                const response = await fetch('{{ApiPrefix}}/config/status');
                const config = await response.json();
                
                if (config.configured) {
                    showToast('配置检查完成');
                    updateOutput('✅ 飞书应用配置已就绪\n\n配置状态: 已配置\nApp ID: 已设置\nApp Secret: 已设置');
                } else {
                    showToast('配置尚未完成', 'error');
                    updateOutput('⚠️ 飞书应用配置未完成\n\n请设置 App ID 和 App Secret');
                }
            } catch (error) {
                showToast('检查配置失败: ' + error.message, 'error');
                updateOutput('❌ 检查配置失败: ' + error.message);
            }
        }
        
        async function reg() {
            const pwd = currentPassword || document.getElementById('pwd').value;
            const code = document.getElementById('approvalCode').value;
            const displayName = document.getElementById('displayName').value;
            
            if (!pwd || !pwd.trim()) {
                showToast('请先验证管理口令', 'error');
                return;
            }
            
            if (!code || !code.trim()) {
                showToast('请输入审批代码', 'error');
                return;
            }
            
            try {
                const response = await fetch('{{ApiPrefix}}/approvals', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-Feishu-Admin-Password': pwd
                    },
                    body: JSON.stringify({
                        approvalCode: code,
                        displayName: displayName || code,
                        description: ''
                    })
                });
                
                if (response.ok) {
                    const result = await response.json();
                    showToast('审批登记成功');
                    updateOutput('✅ 审批流程登记成功\n\n审批代码: ' + code + '\n显示名称: ' + (displayName || code) + '\n登记ID: ' + result.data);
                } else if (response.status === 401) {
                    showToast('管理口令验证失败', 'error');
                    updateOutput('❌ 认证失败: 管理口令不正确');
                } else {
                    const errorText = await response.text();
                    showToast('登记失败: ' + errorText, 'error');
                    updateOutput('❌ 登记失败: ' + errorText);
                }
            } catch (error) {
                showToast('网络错误: ' + error.message, 'error');
                updateOutput('❌ 网络错误: ' + error.message);
            }
        }
        
        async function loadRegistrations() {
            try {
                const response = await fetch('{{ApiPrefix}}/approvals');
                const approvals = await response.json();
                
                if (approvals && approvals.length > 0) {
                    const list = approvals.map(a => `• ${a.displayName} (${a.approvalCode})`).join('\n');
                    updateOutput('📋 已登记的审批流程:\n\n' + list);
                } else {
                    updateOutput('📋 暂无已登记的审批流程\n\n请先登记审批代码');
                }
            } catch (error) {
                showToast('加载失败: ' + error.message, 'error');
                updateOutput('❌ 加载失败: ' + error.message);
            }
        }
        
        async function gen() {
            const code = document.getElementById('code').value;
            const className = document.getElementById('className').value;
            
            if (!code || !code.trim()) {
                showToast('请输入审批代码', 'error');
                return;
            }
            
            try {
                let url = '{{ApiPrefix}}/codegen/' + encodeURIComponent(code);
                if (className && className.trim()) {
                    url += '?className=' + encodeURIComponent(className.trim());
                }
                const response = await fetch(url);
                
                if (response.ok) {
                    const generatedCode = await response.text();
                    showToast('代码生成成功');
                    updateOutput('🚀 生成的 C# 实体类代码:\n\n' + generatedCode);
                } else if (response.status === 404) {
                    showToast('审批代码未找到', 'error');
                    updateOutput('❌ 审批代码未找到: ' + code + '\n\n请确保:\n1. 审批代码正确\n2. 已在飞书中创建对应审批\n3. 飞书应用配置正确');
                } else {
                    const errorText = await response.text();
                    showToast('生成失败: ' + errorText, 'error');
                    updateOutput('❌ 生成失败: ' + errorText);
                }
            } catch (error) {
                showToast('网络错误: ' + error.message, 'error');
                updateOutput('❌ 网络错误: ' + error.message);
            }
        }
    </script>
</body>
</html>